//
//  CommendTribeCell.m
//  yigou
//
//  Created by SunChao on 17/1/11.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "CommendTribeCell.h"
#import "TribeHeader.h"
#import "MyAddTribeListViewController.h"
#import "SearchResultController.h"
@implementation CommendTribeCell
- (IBAction)addAction:(UIButton *)sender {
    NSLog(@"点击了推荐部落");
    if (!sender.selected) {
        [self loadNetworkData];
    }else{
        return;
    }
}

- (void)setFrame:(CGRect)frame
{
    [self getCurrentVC];
//   [[self getCurrentVC]isKindOfClass:[MyAddTribeListViewController class]]||[[self getCurrentVC]isKindOfClass:[SearchResultController class]]
    if ([[self getCurrentVC]isKindOfClass:[MyAddTribeListViewController class]]) {
        NSLog(@"getCurrentVC:%@",[self getCurrentVC]);
        self.addBtn.hidden = YES;
        CGFloat kTableTopBorderWidth = 10;
        CGFloat kTableViewCellMargin = 10;
        frame.origin.y += kTableTopBorderWidth;
        frame.size.height -= kTableViewCellMargin;
    }
    [super setFrame:frame];
}

//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            NSLog(@"nextResponder:%@",nextResponder);
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

-(void)setModel:(CommendTribeModel *)model
{
    _model = model;
    [self.addBtn setTintColor:[UIColor whiteColor]];
    [self.addBtn setTitle:@"+" forState:UIControlStateNormal];
    [self.addBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.addBtn setImage:[UIImage imageNamed:@"对号"] forState:UIControlStateSelected];
    [self.addBtn setTitle:@"" forState:UIControlStateSelected];
    if (_model.is_join == YES) {
        [self.addBtn setBackgroundColor:[UIColor grayColor]];
        self.addBtn.selected = YES;
    }else{
        [self.addBtn setBackgroundColor:QINGColor];
        self.addBtn.selected = NO;
    }
    
    self.addBtn.layer.cornerRadius = 3;
    self.addBtn.layer.masksToBounds =YES;
    self.addBtn.hidden = NO;
    self.nameLabel.text = model.tribe_name;
    [self.bgImg sd_setImageWithURL:[NSURL URLWithString:model.tribe_logo] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    self.bgImg.layer.cornerRadius = 8;
    self.bgImg.layer.masksToBounds = YES;
    self.descLabel.text = model.tribe_desc;
    self.otherLabel.text = [NSString stringWithFormat:@"%@人加入/%@篇内容",model.tribe_popularity,model.tribe_publish];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)fillWithData:(MMMyAddTribe *)data {
    if (!data)
        return;
    self.selectionStyle = UITableViewCellSelectionStyleDefault;
    self.addBtn.hidden = YES;
    self.nameLabel.text = data.tribe_name;
    [self.bgImg sd_setImageWithURL:[NSURL URLWithString:data.tribe_logo] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    self.bgImg.layer.cornerRadius = 8;
    self.bgImg.layer.masksToBounds = YES;
    self.descLabel.text = data.tribe_desc;
    self.otherLabel.text = [NSString stringWithFormat:@"%@人加入/%@篇内容",data.tribe_popularity,data.tribe_publish];
}
- (void)loadNetworkData {
    
    DYBaseRequest *request = [DYBaseRequest new];
        NSString *url = [NSString stringWithFormat:@"%@/hytribe/joinTribe?tribe_id=%@", APIYIGOU, _model.tribe_id];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
       NSLog(@"------%@",request);
        self.addBtn.selected = YES;
       [ViewFactory showSuccess:@"加入成功"];
        [self.addBtn setBackgroundColor:[UIColor grayColor]];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"joinTribeNotification" object:nil];
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        
    }];
}

@end
